package stackDemo;



public class MyStack implements IStack{

    private static final int DEFAULT_NUMS = 10;
    private int[] stack;
    private int useSize;

    public MyStack() {
        this.stack = new int[DEFAULT_NUMS];
        this.useSize = 0;
    }

    @Override
    public void push(int n) throws Exception {
        if(full()) {
            throw new Exception();
        }
        stack[useSize++] = n;
    }

    @Override
    public int pop() throws Exception{
        if(empty()) {
            throw new Exception();
        }
        useSize--;
        int t = stack[useSize];
//        stack[useSize] = null; 如果是引用数据类型，那么就要置为null
        return t;
    }

    @Override
    public int peek() throws Exception {
        if(empty()) {
            throw new Exception();
        }
        return stack[useSize - 1];
    }

    @Override
    public int size() {
        return useSize;
    }

    @Override
    public boolean empty() {
        return useSize == 0;
    }

    @Override
    public boolean full() {
        return useSize == DEFAULT_NUMS;
    }
}
